<html>
<head><meta charset="utf-8"><title>Deferencing Bad Value UB Reference Description · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html">Deferencing Bad Value UB Reference Description</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232697801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232697801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232697801">(Apr 01 2021 at 03:19)</a>:</h4>
<p>I am moving this subtopic out of the raw ref semantics conversation since it doesn't depend on it. I don't know how to push conversations around nor do I want to interrupt the flow of that one, so I'll copy the relevant message here:</p>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/raw.20ref.20semantics/near/232646288">said</a>:</p>
<blockquote>
<p>I propose to change the wording to something like</p>
<blockquote>
<p>Dereferencing (i.e., the <code>*expr</code> <em>place</em> expression) a dangling or unaligned raw pointer.</p>
</blockquote>
<p>would that help?</p>
</blockquote>
<p>I counter-propose being very very specific:</p>
<div class="codehilite" data-code-language="markdown"><pre><span></span><code>Evaluating a [dereference expression] on a [raw pointer] value that is [dangling] or unaligned.

&lt;&lt; And then have examples of both here, or if we want to keep the list to just normative information, put examples after the list &gt;&gt;
</code></pre></div>



<a name="232722237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232722237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232722237">(Apr 01 2021 at 08:38)</a>:</h4>
<p>I would like it to say <code>[dereference place expression]</code> there</p>



<a name="232722286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232722286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232722286">(Apr 01 2021 at 08:38)</a>:</h4>
<p>"Evaluating a [dereference expression]" again sounds like <code>place2value(*ptr)</code> to me</p>



<a name="232722356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232722356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232722356">(Apr 01 2021 at 08:39)</a>:</h4>
<p>especially since "evaluating a place expression" is a bit of a weird operation, considering that for the most part nothing actually <em>happens</em></p>



<a name="232723720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232723720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232723720">(Apr 01 2021 at 08:52)</a>:</h4>
<p>Dereference is always a place expression, no?</p>



<a name="232724573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232724573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232724573">(Apr 01 2021 at 09:01)</a>:</h4>
<p>Evaluating a place expression gives you a location in memory. Although the expressions page could be more explicit on that.</p>



<a name="232724960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232724960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232724960">(Apr 01 2021 at 09:04)</a>:</h4>
<p>I don't know if you can say that dereferencing is a place expression in statements like <code>let x = *p</code> or <code>*p + 2</code></p>



<a name="232725016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232725016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232725016">(Apr 01 2021 at 09:04)</a>:</h4>
<p>You can and we do.</p>



<a name="232725029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232725029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232725029">(Apr 01 2021 at 09:05)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nightly/reference/expressions.html#moved-and-copied-types">https://doc.rust-lang.org/nightly/reference/expressions.html#moved-and-copied-types</a></p>



<a name="232725092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232725092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232725092">(Apr 01 2021 at 09:05)</a>:</h4>
<p>I've said this already, but there is an ambiguity because if I highlight the span of the place <code>*p</code> and the value <code>*p</code> exactly the same bytes are selected</p>



<a name="232725131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232725131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232725131">(Apr 01 2021 at 09:05)</a>:</h4>
<p>because <code>place2value</code> is invisible</p>



<a name="232725349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232725349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232725349">(Apr 01 2021 at 09:07)</a>:</h4>
<p>you could use <code>{ *p }</code> but I'm not sure that this idiom is very transparent</p>



<a name="232725737"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232725737" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232725737">(Apr 01 2021 at 09:10)</a>:</h4>
<p>place2value is literally "using a place expression in a value expression context". But evaluating a place expression in place expression context is still evaluating the place expression.</p>



<a name="232726349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232726349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232726349">(Apr 01 2021 at 09:17)</a>:</h4>
<p>I agree with you. I just think that the documentation needs to be more explicit about this, because many people don't understand place expressions at this level of precision, and in particular I usually hear the term "dereference a pointer" to mean "load memory at a pointer location", and the fact that "construct a place expression from a pointer" uses exactly the same syntax isn't helping matters</p>



<a name="232730203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232730203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232730203">(Apr 01 2021 at 09:48)</a>:</h4>
<p>Yeah. Next time I write stuff, I'll focus on making that all more clear and explicit</p>



<a name="232730338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232730338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232730338">(Apr 01 2021 at 09:50)</a>:</h4>
<p>In the expressions chapter itself.</p>



<a name="232922321"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232922321" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232922321">(Apr 02 2021 at 16:50)</a>:</h4>
<blockquote>
<p>I counter-propose being very very specific:</p>
</blockquote>
<p>That seems to be almost the same, just avoiding the parenthetical? I agree with that part; I am not sure I like the changes to the 2nd half of the sentence but I'm not fundamentally opposed either.<br>
Wrt having examples, we probably should have that for all the UB. I am not sure what would be a good way to add that information to the page.</p>



<a name="232998168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232998168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232998168">(Apr 03 2021 at 10:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="296643">isHavvy</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description/near/232723720">said</a>:</p>
<blockquote>
<p>Dereference is always a place expression, no?</p>
</blockquote>
<p>in a sense it is both a place expression and a value expression... at least it can be used in syntactic positions that expect either. when used in value position, a <code>place2value</code> coercion is implicitly inserted.</p>



<a name="232998174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232998174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232998174">(Apr 03 2021 at 10:52)</a>:</h4>
<p>I created a PR to adjust the wording: <a href="https://github.com/rust-lang/reference/pull/1000">https://github.com/rust-lang/reference/pull/1000</a></p>



<a name="232998176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/232998176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#232998176">(Apr 03 2021 at 10:52)</a>:</h4>
<p>(Yay PR 1000 :D)</p>



<a name="233000570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/233000570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#233000570">(Apr 03 2021 at 11:39)</a>:</h4>
<p>I am way too tired to review tonight, but hopefully tomorrow. No later than Monday (I don't work Monday)</p>



<a name="233000941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/233000941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#233000941">(Apr 03 2021 at 11:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="296643">isHavvy</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description/near/233000570">said</a>:</p>
<blockquote>
<p>I am way too tired to review tonight, but hopefully tomorrow. No later than Monday (I don't work Monday)</p>
</blockquote>
<p>no worries, this is not urgent</p>



<a name="233258204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing%20Bad%20Value%20UB%20Reference%20Description/near/233258204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Deferencing.20Bad.20Value.20UB.20Reference.20Description.html#233258204">(Apr 06 2021 at 02:28)</a>:</h4>
<p>I hope <a href="https://github.com/rust-lang/reference/pull/1003/files">https://github.com/rust-lang/reference/pull/1003/files</a> is a step in the right direction.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>